#include "asgkaDelete.h"
#include "asgka_space.h"
#include "asgka_common.h"
#include "common.h"

asgkaDelete::asgkaDelete()
:asgkaBase(),
 m_elementDecryptKey(NULL),
 m_elementGroupKeyR(NULL),
 m_elementGroupKeyA(NULL)
{
}

asgkaDelete::asgkaDelete(singleGroup *group,
						 c_asgka_element elementDeckey)
:asgkaBase(group),
 m_elementGroupKeyR(NULL),
 m_elementGroupKeyA(NULL)
{
	asgka_malloc(m_elementDecryptKey);
	memcpy(m_elementDecryptKey, elementDeckey, LENGTH);
}

asgkaDelete::asgkaDelete(singleGroup *group, 
						 c_asgka_element elementGrpkeyR,
						 c_asgka_element elementGrpkeyA)
:asgkaBase(group),
 m_elementDecryptKey(NULL)
{
	asgka_malloc(m_elementGroupKeyR);
	asgka_malloc(m_elementGroupKeyA);

	memcpy(m_elementGroupKeyR, elementGrpkeyR, LENGTH);
	memcpy(m_elementGroupKeyA, elementGrpkeyA, LENGTH);
}

asgkaDelete::~asgkaDelete()
{
	if (m_elementDecryptKey != NULL)
		asgka_free(m_elementDecryptKey);
	if (m_elementGroupKeyR != NULL)
		asgka_free(m_elementGroupKeyR);
	if (m_elementGroupKeyA != NULL)
		asgka_free(m_elementGroupKeyA);
}

void asgkaDelete::setElementDecryptKey(c_asgka_element elementDeckey)
{
	if (m_elementDecryptKey == NULL)
		asgka_malloc(m_elementDecryptKey);
	memcpy(m_elementDecryptKey, elementDeckey, LENGTH);

}

void asgkaDelete::setElementGroupKeys(c_asgka_element elementGrpkeyR,
								   c_asgka_element elementGrpkeyA)
{
	if (m_elementGroupKeyR == NULL)
		asgka_malloc(m_elementGroupKeyR);
	if (m_elementGroupKeyA == NULL)
		asgka_malloc(m_elementGroupKeyA);

	memcpy(m_elementGroupKeyR, elementGrpkeyR, LENGTH);
	memcpy(m_elementGroupKeyA, elementGrpkeyA, LENGTH);
}

void asgkaDelete::computeMyDecryptKey()
{
	if (m_group == NULL)
		return;
	if (m_elementDecryptKey == NULL)
		return;

	asgka_element oldDecKey;
	asgka_malloc(oldDecKey);

	memcpy(oldDecKey, m_group->getAddrofDecryptKey(), LENGTH);

	asgkaDivG2(m_my_decrypt_key, oldDecKey, m_elementDecryptKey, LENGTH);
	asgka_free(oldDecKey);
}

void asgkaDelete::computeMyGroupKeyR()
{
	if (m_group == NULL)
		return ;
	if (m_elementGroupKeyR == NULL)
		return;
	
	asgka_element oldGroupR;
	asgka_malloc(oldGroupR);

	memcpy(oldGroupR, m_group->getAddrofGroupKeyR(), LENGTH);

	asgkaDivG2(m_my_groupKeyR, oldGroupR, m_elementGroupKeyR, LENGTH);
	asgka_free(oldGroupR);
}

void asgkaDelete::computeMyGroupKeyA()
{
	if (m_group == NULL)
		return ;
	if (m_elementGroupKeyA == NULL)
		return;
	
	asgka_element oldGroupA;
	asgka_malloc(oldGroupA);

	memcpy(oldGroupA, m_group->getAddrofGroupKeyA(), LENGTH);

	asgkaDivGT(m_my_groupKeyA, oldGroupA, m_elementGroupKeyA, LENGTH);
	asgka_free(oldGroupA);
}
